package com.platform.qujia.controller;


import com.platform.qujia.dao.AppUserInfoMapper;
import com.platform.qujia.dao.LabelMapper;
import com.platform.qujia.dao.SysDictDataMapper;
import com.platform.qujia.pojo.*;
import com.platform.qujia.service.BusinessAuditService;
import com.platform.qujia.utils.StringUtils;
import com.platform.qujia.utils.exception.CustomException;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (BusinessAudit)表控制层
 *
 * @author makejava
 * @since 2023-12-19 16:26:28
 */
@RestController
@RequestMapping("businessAudit")
@Api(tags = "审核")
public class BusinessAuditController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessAuditService businessAuditService;

    @Resource
    private LabelMapper labelMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private AppUserInfoMapper appUserInfoMapper;



    /**
     * 分页查询
     *
     * @param businessAudit 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<BusinessAudit>> queryByPage(BusinessAudit businessAudit, PageRequest pageRequest) {
        return ResponseEntity.ok(this.businessAuditService.queryByPage(businessAudit, pageRequest));
    }

    @PostMapping("/query")
    public ResponseEntity<List<BusinessAudit>> query(BusinessAudit businessAudit) {
        return ResponseEntity.ok(this.businessAuditService.query(businessAudit));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BusinessAudit> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.businessAuditService.queryById(id));
    }

    @PostMapping("/queryLatestAudit")
    public ResponseEntity<Map> queryLatestAudit(@RequestBody BusinessAudit businessAudit){
        Map<String,Object> m=new HashMap<>();
        Long userId = businessAudit.getUserId();
        AppUserInfo appUserById = appUserInfoMapper.getAppUserById(userId);
        if(StringUtils.isEmpty(businessAudit.getBusinessName())||
        businessAudit.getUserId()==null){
            throw new CustomException("参数错误");
        }

        businessAudit = this.businessAuditService.queryLatestAudit(businessAudit);

        m.put("code",200);
        m.put("msg","success");
        if(businessAudit==null||!businessAudit.getStatus().equals("PASS")){
            if(appUserById!=null&&StringUtils.isNotEmpty(appUserById.getMobilePhone())){
                //List<LabelRule> labelRuleList=labelMapper.getLabelRuleByPhone(appUserById.getMobilePhone());
                CustomerLabel customerLabel=new CustomerLabel();
                customerLabel.setPhone(appUserById.getMobilePhone());
                customerLabel.setEnterpriseId(appUserById.getStationId());
                customerLabel.setDelFlag("0");
                List<CustomerLabel> customerLabelList = labelMapper.getCustomerLabelList(customerLabel);
                if(customerLabelList.size()>0){
                    businessAudit=new BusinessAudit();
                    businessAudit.setStatus("PASS");
                    businessAudit.setAuditTime(customerLabelList.get(0).getCreateTime());
                    businessAudit.setCreateTime(customerLabelList.get(0).getCreateTime());
                    m.put("result",businessAudit);
                }
            }
        }
        if(businessAudit != null){
            m.put("result",businessAudit);
        }else{
            m.put("result",null);
        }
        return ResponseEntity.ok(m);
    }

    /**
     * 新增数据
     *
     * @param businessAudit 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Map> add(@RequestBody BusinessAudit businessAudit) {
        if(StringUtils.isEmpty(businessAudit.getBusinessName())||businessAudit.getBusinessId()==null
        ||businessAudit.getEnterpriseId()==null||StringUtils.isEmpty(businessAudit.getMobilePhone())){
            throw new CustomException("参数错误");
        }
        BusinessAudit auditQuery=new BusinessAudit();
        auditQuery.setMobilePhone(businessAudit.getMobilePhone());
        List<BusinessAudit> list=businessAuditService.queryByMobilePhone(businessAudit.getMobilePhone());
        if(list.size()>0){
            Map<String,Object> m=new HashMap<>();
            m.put("code",500);
            m.put("msg","该手机号已经在审核中或审核通过");
            return ResponseEntity.ok(m);
        }
        CustomerLabel customerLabel = new CustomerLabel();
        customerLabel.setPhone(businessAudit.getMobilePhone());
        customerLabel.setEnterpriseId(businessAudit.getEnterpriseId().intValue());
        customerLabel.setLabelId(businessAudit.getBusinessId()+"");
        List<CustomerLabel> customerLabelList = labelMapper.getCustomerLabelList(customerLabel);
        if(customerLabelList.size()>0){
            Map<String,Object> m=new HashMap<>();
            m.put("code",500);
            m.put("msg","该手机号已经加入渠道");
            return ResponseEntity.ok(m);
        }

        businessAudit.setCreateTime(new Date());
        businessAudit.setStatus("IN_REVIEW");
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }

        String ordNo = System.currentTimeMillis() + result;
        businessAudit.setOrderNo(ordNo);
        BusinessAudit insert = this.businessAuditService.insert(businessAudit);
        Map<String,Object> m=new HashMap<>();
        m.put("code",200);
        m.put("result",insert);
        m.put("msg","success");
        return ResponseEntity.ok(m);
    }

    /**
     * 编辑数据
     *
     * @param businessAudit 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BusinessAudit> edit(BusinessAudit businessAudit) {
        return ResponseEntity.ok(this.businessAuditService.update(businessAudit));
    }

    @GetMapping("/queryLabelRuleType")
    public ResponseEntity<Map> queryLabelRuleType(){
        List<SysDictData> label_rule_type = sysDictDataMapper.selectDictDataByType("label_rule_type");
        Map<String,Object> m=new HashMap<>();
        m.put("code",200);
        m.put("result",label_rule_type);
        m.put("msg","success");
         return ResponseEntity.ok(m);
    }

    /**
     * 删除数据
     *

     * @return 删除是否成功
     */
/*    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.businessAuditService.deleteById(id));
    }*/
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<String> handleUserNotFoundException(CustomException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
    }

}

